package sixth.group.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sixth.group.domain.Dishes;
import sixth.group.domain.Restaurant;
import sixth.group.service.DishesService;

import javax.servlet.http.HttpSession;

@Controller
//Controller和用户交互， 用户一般HTTP协议访问Controller，给不同的Controller和不同的方法映射不同的url地址，进行匹配
@RequestMapping(value = "/dishes")
public class DishesController {
    @Autowired
    private DishesService dishesService;

    @RequestMapping(value = "/insert")
    public ModelAndView insert(Dishes dishes, HttpSession httpSession)
    {
        ModelAndView modelAndView = new ModelAndView();
        Restaurant restaurant = (Restaurant)httpSession.getAttribute("RESTAURANT_SESSION");
        //从当前登录账户中读取rid赋给dishes的drid
        dishes.setDrid(restaurant.getRid());
        System.out.println(dishes.toString());

        boolean b = dishesService.dishInsert(dishes);
        if (b){
            modelAndView.setViewName("redirect:../insertdishessuc.html");
        }
        else {
            modelAndView.setViewName("redirect:../insertdishesfail.html");
        }
        return modelAndView;
    }

    @RequestMapping(value = "/delete")
    public ModelAndView delete(@RequestParam Integer did){
        ModelAndView modelAndView = new ModelAndView();
        Integer i = dishesService.dishDelete(did);
        System.out.println(i);

        //跳转删除成功页面
        modelAndView.setViewName("redirect:../deletedishessuc.html");

        return modelAndView;
    }

    @RequestMapping(value = "/change1step")
    public ModelAndView change1step(@RequestParam Integer did,HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        //因为修改信息后页面生成的dishes对象只有dname、dvalue、dintroduction三个属性，
            // 需要从点击的修改页面把did传进来然后根据did找到要修改的相应的dishes对象后将修改页面中没有的
                // did和drid先存入session中
        Dishes dishes = dishesService.selectbydid(did);

        httpSession.setAttribute("chgdishdid",dishes.getDid());
        httpSession.setAttribute("chgdishdrid",dishes.getDid());

        //跳转修改信息的页面
        modelAndView.setViewName("redirect:../disheschginfo.html");

        return modelAndView;
    }

    @RequestMapping(value = "/change")
    public ModelAndView change(Dishes dishes,HttpSession httpSession){
        ModelAndView modelAndView = new ModelAndView();
        //完善页面生成的dishes对象的drid、did属性

        System.out.println(dishes.toString());

        Integer drid = (Integer) httpSession.getAttribute("chgdishdrid");
        Integer did = (Integer) httpSession.getAttribute("chgdishdid");
        dishes.setDrid(drid);
        dishes.setDid(did);

        System.out.println(dishes.toString());

        boolean b = dishesService.dishUpdate(dishes);
        //跳转修改信息的页面
        if (b)
        {
            modelAndView.setViewName("redirect:../changedishessuc.html");
        }

        return modelAndView;
    }
}
